package utils

import (
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"time"
)

var Logger *log.Logger

func InitLogger() error {
	// 创建logs目录
	logDir := "logs"
	if err := os.MkdirAll(logDir, 0755); err != nil {
		return err
	}

	// 创建日志文件，按日期命名
	logFile := filepath.Join(logDir, fmt.Sprintf("%s.log", time.Now().Format("2006-01-02")))
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return err
	}
	// 同时输出到文件和控制台
	multiWriter := io.MultiWriter(file, os.Stdout)
	Logger = log.New(multiWriter, "", log.Ldate|log.Ltime)

	// 写入启动日志
	Logger.Println("=== 日志系统初始化成功 ===")
	return nil

}
